package service

import (
	"context"
	"fmt"

	"admin_template/bff/admin/hertz_gen/base"
	"admin_template/bff/admin/internal/pkg/utils"
	"admin_template/bff/admin/internal/schema"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func (s *Service) UpdateStateFunc(
	face Face,
	module string,
	ctx context.Context,
	c *app.RequestContext) {
	var err error
	var req base.StateUpdateRequest
	ok := s.BindAndValidate(ctx, c, &req)
	if !ok {
		return
	}
	err = face.StateUpdate(ctx, &req)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			module,
			fmt.Sprintf("%s StateUpdate", module),
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)
}

func (s *Service) DelFunc(
	face Face,
	module string,
	ctx context.Context,
	c *app.RequestContext) {
	var err error
	var req base.IDRequest
	ok := s.BindAndValidate(ctx, c, &req)
	if !ok {
		return
	}
	err = face.StateUpdate(ctx, &base.StateUpdateRequest{
		ID:    req.ID,
		State: 2,
	})
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			module,
			fmt.Sprintf("%s StateUpdate", module),
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)
}
func (s *Service) AddFunc(
	face Face,
	params any,
	module, msg string,
	ctx context.Context,
	c *app.RequestContext) {
	ok := s.BindAndValidate(ctx, c, params)
	if !ok {
		return
	}
	err := face.Add(ctx, params)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			fmt.Sprintf("%s Add", module),
			msg,
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)
}

func (s *Service) UpdateInfoFunc(
	face Face,
	params any,
	module, msg string,
	ctx context.Context,
	c *app.RequestContext) {
	ok := s.BindAndValidate(ctx, c, params)
	if !ok {
		return
	}
	err := face.UpdateInfo(ctx, params)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			fmt.Sprintf("%s UpdateInfo", module),
			msg,
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)
}
func (s *Service) ListFunc(
	face Face,
	params any,
	module string,
	ctx context.Context,
	c *app.RequestContext) {
	ok := s.BindAndValidate(ctx, c, params)
	if !ok {
		return
	}
	total, page, size, list, err := face.List(ctx, params)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			fmt.Sprintf("%s List", module),
			"",
			s.CtxLogger, err)
		return
	}
	reply := schema.ListReply{
		Code: base.ErrCode_Success,
		Msg:  "获取成功",
		Data: &schema.ListData{
			Total: total,
			Page:  page,
			Size:  size,
			List:  list,
		},
	}
	utils.Reply(c, consts.StatusOK, &reply)
}
func (s *Service) AllFunc(
	face Face,
	params any,
	module string,
	ctx context.Context,
	c *app.RequestContext) {
	ok := s.BindAndValidate(ctx, c, params)
	if !ok {
		return
	}
	list, err := face.All(ctx, params)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			fmt.Sprintf("%s All", module),
			"",
			s.CtxLogger, err)
		return
	}
	reply := schema.AllReply{
		Code: base.ErrCode_Success,
		Msg:  "获取成功",
		Data: &schema.AllData{List: list},
	}
	utils.Reply(c, consts.StatusOK, &reply)
}
